﻿using IdGen;
using LmgCMS.Middleware;
using LmgCMS.Service;
using Microsoft.AspNetCore.Mvc;
using System.Text;

namespace LmgCMS.Controllers
{
    [TempleteFilter]
    [ApiExplorerSettings(IgnoreApi = true)]
    public class NewsController : Controller
    {
        private readonly INewsService _newsService;
        private readonly INewsDataService _newsdataService;
        private readonly IWebHostEnvironment _webHostEnvironment;
        private readonly ISiteService _siteService;
        public NewsController(INewsService newsService, INewsDataService newsdataService, ISiteService siteService, IWebHostEnvironment webHostEnvironment)
        {
            _newsService = newsService;
            _newsdataService = newsdataService;
            _siteService = siteService;
            _webHostEnvironment = webHostEnvironment;
        }
        /// <summary>
        /// 首页
        /// </summary>
        [Route("News/{channelName}")]
        public IActionResult Index(string type = "")
        {
            ViewBag.type = type;
            return View();
        }
        /// <summary>
        /// 列表页
        /// </summary>
        [Route("News/{channelName}/List/{id?}")]
        public IActionResult List([FromRoute] string id)
        {
            ViewBag.id = id;
            return View(id);
        }
        /// <summary>
        /// 详情页
        /// </summary>
        [Route("News/{channelName}/Show/{id}")]
        public IActionResult Show([FromRoute] string id)
        {
            ViewBag.id = id;
            return View(id);
        }

        #region CreateSiteRss
        /// <summary>
        /// 获取RSS
        /// </summary>
        /// <param name="category">分类</param>
        /// <returns></returns>
        [Route("News/Rss/{category}")]
        [HttpGet]
        public IActionResult Rss(string category)
        {
            string url = HttpContext.Request.Host.Host;
            string siteName = "luomingui";
            string desc = "";
            var siteInfo = _siteService.Find(x => x.SiteUrl == url && x.Status== 1);
            if (siteInfo != null)
            {
                siteName = siteInfo.Name;
                desc = siteInfo.SeoDescribe;
            }
            var articleList = _newsService.GetNewsLast(category, 51);  //获取需要生成sitemmap的数据
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("<rss version=\"2.0\"> ");
            builder.AppendLine("  <channel> ");
            builder.AppendLine("    <title>" + siteName + "</title> ");
            builder.AppendLine("    <link>" + url + "</link> ");
            builder.AppendLine("    <description> " + desc + " </description> ");
            foreach (var item in articleList)
            {
                var spriderUrl = url + string.Format("/News/Faq/Show/{0}", item.ID);
                builder.AppendLine("    <item> ");
                builder.AppendLine("      <title>" + item.Title + "</title> ");
                builder.AppendLine("      <description>" + item.SmallText + "</description> ");
                builder.AppendLine("      <link>" + spriderUrl + "</link> ");
                builder.AppendLine("      <pubDate>" + item.AddTime.ToString6() + " </pubDate> ");
                builder.AppendLine("    </item> ");
            }
            builder.AppendLine("  </channel> ");
            builder.AppendLine("</rss> ");
            return new ContentResult
            {
                Content = builder.ToString(),
                ContentType = "application/xml",
                StatusCode = 200
            };
        }
        #endregion

    }
}
